package com.example.mymvvmdemo.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.mymvvmdemo.MyApplication;
import com.example.mymvvmdemo.constants.Constants;


public class SpUtils {
    private static SpUtils spUtils;
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor edit;

    public static String getString(String key, String defalueValue){
        if (MyApplication.getInstance() == null){
            LogUtils.d("SpUtils getString context empty");
            return null;
        }
        return getSpUtils(MyApplication.getInstance(), Constants.ACCOUNT_NAME).getStringValue(key, defalueValue);
    }

    public static SpUtils getSpUtils(Context context, String name) {
        synchronized (String.class) {
            if (spUtils == null) {
                synchronized (String.class) {
                    spUtils = new SpUtils();
                    initSharedPreferences(context, name);
                }
            }
        }
        return spUtils;
    }

    @SuppressLint("CommitPrefEdits")
    private static void initSharedPreferences(Context context, String name) {
        synchronized (String.class) {
            if (sharedPreferences == null) {
                synchronized (String.class) {
                    sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
                    edit = sharedPreferences.edit();
                }
            }
        }
    }

    public boolean putValue(String key, Object value) {
        if (value instanceof String)
            edit.putString(key, (String) value);
        if (value instanceof Boolean)
            edit.putBoolean(key, (Boolean) value);
        if (value instanceof Integer)
            edit.putInt(key, (Integer) value);
        if (value instanceof Long)
            edit.putLong(key, (Long) value);
        return edit.commit();
    }

    public Long getLongValue(String key, long delValue) {
        if (key == null)
            return null;
        return sharedPreferences.getLong(key, delValue);
    }

    public String getStringValue(String key, String delValue) {
        if (key == null)
            return null;
        return sharedPreferences.getString(key, delValue);
    }

    public Boolean getBooleanValue(String key) {
        if (key == null)
            return false;
        else
            return sharedPreferences.getBoolean(key, false);
    }

    public Boolean getBooleanValue(String key,boolean defaultBool) {
        if (key == null)
            return false;
        else
            return sharedPreferences.getBoolean(key, defaultBool);
    }

    public Integer getIntValue(String key, int defaultValue) {
        if (key == null)
            return 0;
        return sharedPreferences.getInt(key, defaultValue);
    }

    public void removeValue(String key) {
        edit.remove(key);
        edit.commit();
    }

}
